1 Environment setup

library(data.table)
library(dplyr)
library(ggmap)
library(graphZoo)
library(RColorBrewer)
library(animation)
library(scales)

cbf <- brewer.pal(8, "Dark2") # A colorblind friendly palette

Back to top


2 Data loading

select_cols <- c(1:4, 8:12, 14:15, 27, 29:30, 35:36, 41:42, 59, 81:82, 98, 101)
col_class <- rep("NULL", 134)
col_class[select_cols] <- NA

gtd <- as.data.table(
  read.csv("../data/globalterrorismdb_0814dist.csv", 
           colClasses = col_class)) %>%
  mutate(idate = ISOdate(iyear, imonth, iday))

Back to top


3 Attacks by country (TOP 50)

attacks <- group_by(gtd, country_txt) %>%
  summarize(number = length(eventid)) %>%
  ungroup() %>%
  mutate(rank = rank(-number))

g <- ggplot(filter(attacks, rank < 51), 
            aes(x = reorder(country_txt, number), y = number)) +
  geom_bar(stat = "identity", color = "white", fill = cbf[2]) + 
  coord_flip() + 
  ggtitle(bquote(atop("Terrorist attacks by country",
                      atop("since 1970")))) + 
  ylab("Number of events") + 
  theme_graphzoo(base_size = 13, family = "Avenir Next") +
  theme(axis.title.y = element_blank())

g <- addBanner(g, font.size = 3, heights = c(1, 0.05*6/9),
          l.txt = "GRAPHZOO.TUMBLR.COM", 
          r.txt = "SOURCE: GLOBAL TERRORISM DATABASE")

g

Figure 1: Number of terrorist attacks by country according to the Global Terrorism Database.

Back to top


4 Casualties by country (TOP 50)

casualties <- group_by(gtd, country_txt) %>%
  summarize(number = sum(nkill, na.rm = TRUE)) %>%
  ungroup() %>%
  mutate(rank = rank(-number))

g <- ggplot(filter(casualties, rank < 51), 
            aes(x = reorder(country_txt, number), y = number)) +
  geom_bar(stat = "identity", color = "white", fill = cbf[2]) + 
  coord_flip() + 
  ggtitle(bquote(atop("Terrorist attacks by country",
                      atop("since 1970")))) + 
  ylab("Number of casualties") + 
  theme_graphzoo(base_size = 13, family = "Avenir Next") +
  theme(axis.title.y = element_blank())

g <- addBanner(g, font.size = 3, heights = c(1, 0.05*6/9),
          l.txt = "GRAPHZOO.TUMBLR.COM", 
          r.txt = "SOURCE: GLOBAL TERRORISM DATABASE")

g

Figure 2: Total number of casualties resulting from terrorist attacks by country according to the Global Terrorism Database.

Back to top


5 Worldwide locations of terrorist attacks since January 2000

gtd_2000 <- filter(gtd, iyear > 1999) %>%
  mutate(nkill = replace(nkill, is.na(nkill), 0),
         nwound = replace(nwound, is.na(nwound), 0)) %>%
  mutate(nvic = nkill + nwound)

vic <- group_by(gtd_2000, idate) %>%
  summarize(count = sum(nvic)) %>%
  arrange(idate) %>%
  mutate(cum_count = cumsum(count))

start <- seq(ISOdate(2000, 1, 1), max(gtd_2000$idate, na.rm = TRUE), by = "1 week")
end <- seq(ISOdate(2000, 1, 8), max(gtd_2000$idate, na.rm = TRUE), by = "1 week")

base <- ggplot() + 
  borders("world", colour = "gray50", size = 1) + 
  coord_fixed() + 
  theme_minimal(base_size = 2.5*18) + 
  theme(legend.position = "top",
        legend.title.align = 0.5,
        axis.line = element_blank(), 
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()) 

ani.options(ani.height = 1440, ani.width = 2560, 
            interval = 1/12, loop = FALSE)

if (!file.exists("notebook_files/figure-html/figure_3.mp4")) {
  saveVideo({
    for (i in 1:length(end)) {
      tmp1 <- filter(gtd_2000, idate >= start[i] & idate < end[i])
      tmp2 <- filter(gtd_2000, idate < end[i])
      
      g <- base + 
        geom_point(data = tmp1, aes(x = longitude, y = latitude, size = nvic), color = "dodgerblue4") +
        geom_point(data = tmp2, aes(x = longitude, y = latitude, size = nvic), color = "tomato3", alpha = 0.1) +
        annotate("text", x = -147, y = 0, label = format(start[i], "%Y-%m-%d"), size = 2.5*4) +     
        scale_size_continuous(name = "Victims\n(deaths and injuries)",
                              limits = c(0, max(gtd_2000$nvic, na.rm = TRUE)),
                              range = 2.5*c(2, 12), 
                              breaks = seq(0, 1250, 250),
                              labels = paste0("≥ ", seq(0, 1250, 250), "   "))
      
      g <- addBanner(g, font.size = 2.5*5, heights = c(1, 0.03*16/9),
                     l.txt = "GRAPHZOO.TUMBLR.COM", 
                     r.txt = "SOURCE: GLOBAL TERRORISM DATABASE")
      
      ins <- ggplot(filter(vic, idate < end[i]), 
                    aes(x = idate, y = cum_count)) +
        geom_line(size = 2.5*2, color = "dodgerblue4") + 
        geom_line(data = vic, size = 2.5*2, color = "tomato3", alpha = 0.1) +
        xlim(start[1], end[length(end)]) + 
        theme_minimal(base_size = 2.5*14) +
        xlab(NULL) + 
        scale_y_continuous(name = "Cumulative number\nof victims\n", labels = comma,
                           limits = c(0, max(vic$cum_count)))
      
      grid.newpage()
      print(g, vp = viewport(width = 1, height = 1, x = 0.5, y = 0.5)) 
      print(ins, vp = viewport(width = 0.29, height = 0.29, x = 0.01, y = 0.35, just = "left"))      
      }
    }, 
    video.name = "notebook_files/figure-html/figure_3.mp4", 
    other.opts = "-vcodec libx264 -vb 20M -pix_fmt yuv420p")
}

cat("<video controls><source src='notebook_files/figure-html/figure_3.mp4' type='video/mp4'>Your browser does not support the video tag.</video>")

Figure 3: Weekly locations of all terrorist attacks since January 2000.

Back to top


6 Locations of terrorist attacks in the US since January 1970

gtd_usa <- filter(gtd, country == 217) %>%
  mutate(nkill = replace(nkill, is.na(nkill), 0),
         nwound = replace(nwound, is.na(nwound), 0)) %>%
  mutate(nvic = nkill + nwound)

g <- ggplot() + 
  borders("state", colour = "gray50") + 
  geom_point(data = gtd_usa, 
             aes(x = longitude, y = latitude, size = nvic),
             color = cbf[2], alpha = 0.1) +
  coord_map("gilbert") +  
  xlim(-125, -66) + ylim(25, 50) +
  ggtitle("Locations of terrorist attacks in the US since 1970\n") +
  theme_graphzoo(base_size = 13, family = "Avenir Next") + 
  theme(legend.position = "bottom",
        legend.title.align = 0.5,
        axis.line = element_blank(), 
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        plot.margin = unit(c(1, 0, 0, 0), "lines")) +
  scale_size_continuous(name = "Number of victims\n(deaths and injuries)",
                        limits = c(0, max(gtd_usa$nvic, na.rm = TRUE)),
                        range = c(4, 14), 
                        breaks = seq(0, 1250, 250),
                        labels = paste0("≥ ", seq(0, 1250, 250), "   "))

g <- addBanner(g, font.size = 4, heights = c(1, 0.05* 9/7),
               l.txt = "GRAPHZOO.TUMBLR.COM", 
               r.txt = "SOURCE: GLOBAL TERRORISM DATABASE")

png("notebook_files/figure-html/figure_4-1.png", bg = '#F0F0F0', width = 9, height = 7, unit = "in", res = 300)
g
dev.off()

Figure 4:Locations of all terrorist attacks in the US since January 1970.

Back to top


7 Locations of terrorist attacks in France since January 1970

gtd_france <- filter(gtd, country == 69 | country == 238) %>%
  mutate(nkill = replace(nkill, is.na(nkill), 0),
         nwound = replace(nwound, is.na(nwound), 0)) %>%
  mutate(nvic = nkill + nwound)

g <- ggplot() + 
  borders("france", colour = "gray50") + 
  geom_point(data = gtd_france, 
             aes(x = longitude, y = latitude, size = nvic),
             color = cbf[2], alpha = 0.1) +
  coord_map("gilbert") +  
  #xlim(-125, -66) + ylim(25, 50) +
  ggtitle("Locations of terrorist attacks in France since 1970\n") +
  theme_graphzoo(base_size = 13, family = "Avenir Next") + 
  theme(legend.position = "bottom",
        legend.title.align = 0.5,
        axis.line = element_blank(), 
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        plot.margin = unit(c(1, 0, 0, 0), "lines")) +
  scale_size_continuous(name = "Number of victims\n(deaths and injuries)",
                        limits = c(0, max(gtd_france$nvic, na.rm = TRUE)),
                        range = c(4, 14), 
                        breaks = seq(0, 75, 25),
                        labels = paste0("≥ ", seq(0, 75, 25), "   "))g <- addBanner(g, font.size = 4, heights = c(1, 0.05* 9/7),
               l.txt = "GRAPHZOO.TUMBLR.COM", 
               r.txt = "SOURCE: GLOBAL TERRORISM DATABASE")

png("notebook_files/figure-html/figure_5-1.png", bg = '#F0F0F0', width = 7, height = 7, unit = "in", res = 300)
g
dev.off()

Figure 5:Locations of all terrorist attacks in France since January 1970.

Back to top